package com.example.demothree.controller;

import com.example.demothree.bean.Customer;
import com.example.demothree.bean.Goods;
import com.example.demothree.bean.Suppliers;
import com.example.demothree.service.SysService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("sys")
public class SysController {
    @Resource
    private SysService sysService;
    private Suppliers suppliers;
    private Customer customer;
    private Long sysId = 0L;
    //商品资料
    @RequestMapping("sysspzl")
    public String sysspzl(Model model,
                          @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<Goods> pagespzl = sysService.sysSpzl(page,size);
        model.addAttribute("pagespzl",pagespzl);
        return "sysspzl";
    }
    //供应商资料
    @RequestMapping("sysgyszl")
    public String sysgyszl(Model model,
                           @RequestParam(value = "page", defaultValue = "0") Integer page,
                           @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.sysId != 0L){//判断是否删除成功
            Long deleteGyszl1 = this.sysId;
            model.addAttribute("deleteGyszl1",deleteGyszl1);
        }
        this.sysId = 0L;
        //分页
        Page<Suppliers> pagegyszl = sysService.sysGyszl(page,size);
        model.addAttribute("pagegyszl",pagegyszl);
        return "sysgyszl";
    }
    //删除供应商
    @GetMapping("sysgyszlDelete/{id}")
    public String sysgyszlDelete(@PathVariable Long id) {
        //按id删除
        sysService.deletegyszl(Long.valueOf(id));
        //删除弹窗
        this.sysId = id;
        return "redirect:/sys/sysgyszl";
    }
    @RequestMapping("addgys")
    public String addgys() {
        return "addgys";
    }
    //添加供应商
    @RequestMapping("addgyszc")
    @ResponseBody
    public String addgyszc(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String zip = request.getParameter("zip");
        String phone = request.getParameter("phone");
        String linkMan = request.getParameter("linkMan");
        String linkTel = request.getParameter("linkTel");
        String bank = request.getParameter("bank");
        String bankAccount = request.getParameter("bankAccount");
        String email = request.getParameter("email");
        suppliers = new Suppliers();
        Suppliers suppliers1 = sysService.findgysByName(name);
        int gysadd = 0;
        if (suppliers1 == null) {
            try {
                suppliers.setSupplierName(name);
                suppliers.setAddress(address);
                suppliers.setZip(zip);
                suppliers.setPhone(phone);
                suppliers.setLinkMan(linkMan);
                suppliers.setLinkTel(linkTel);
                suppliers.setBank(bank);
                suppliers.setBankAccount(Long.valueOf(bankAccount));
                suppliers.setEmail(email);
                sysService.addgys(suppliers);
                gysadd = 100;
            } catch (Exception e)  {
                gysadd = 200;
                e.printStackTrace();
            }
        }
        response.getWriter().print(gysadd);
        return null;
    }
    //客户资料
    @RequestMapping("syskhzl")
    public String syskhzl(Model model,
                          @RequestParam(value = "page", defaultValue = "0") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.sysId != 0L){//判断是否删除成功
            Long deleteKhzl1 = this.sysId;
            model.addAttribute("deleteKhzl1",deleteKhzl1);
        }
        this.sysId = 0L;
        //分页
        Page<Customer> pagekhzl = sysService.sysKhzl(page,size);
        model.addAttribute("pagekhzl",pagekhzl);
        return "syskhzl";
    }
    //删除客户
    @GetMapping("syskhzlDelete/{id}")
    public String syskhzlDelete(@PathVariable Long id) {
        //按id删除
        sysService.deletekhzl(Long.valueOf(id));
        //删除弹窗
        this.sysId = id;
        return "redirect:/sys/syskhzl";
    }
    //添加客户
    @RequestMapping("addkh")
    public String addkh() {
        return "addkh";
    }
    @RequestMapping("addkhzc")
    @ResponseBody
    public String addkhzc(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String zip = request.getParameter("zip");
        String phone = request.getParameter("phone");
        String linkMan = request.getParameter("linkMan");
        String linkTel = request.getParameter("linkTel");
        String bank = request.getParameter("bank");
        String bankAccount = request.getParameter("bankAccount");
        String email = request.getParameter("email");
        customer = new Customer();
        Suppliers suppliers1 = sysService.findgysByName(name);
        int khadd = 0;
        if (suppliers1 == null) {
            try {
                customer.setName(name);
                customer.setAddress(address);
                customer.setZip(zip);
                customer.setPhone(phone);
                customer.setLinkMan(linkMan);
                customer.setLinkTel(linkTel);
                customer.setBank(bank);
                customer.setBankAccount(Long.valueOf(bankAccount));
                customer.setEmail(email);
                sysService.addkh(customer);
                khadd = 100;
            } catch (Exception e)  {
                khadd = 200;
                e.printStackTrace();
            }
        }
        response.getWriter().print(khadd);
        return null;
    }
}
